package cn.com.anysdk.starter.config.redis;

import cn.com.anysdk.redis.api.IRedisService;
import cn.com.anysdk.redis.event.RedisEventManager;
import cn.com.anysdk.redis.factory.RedisServiceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * AnySDK Redis自动配置类
 * 负责创建IRedisService bean和相关依赖
 */
@Configuration
@EnableConfigurationProperties(RedisProperties.class)
public class RedisAutoConfiguration {

    private static final Logger log = LoggerFactory.getLogger(RedisAutoConfiguration.class);

    public RedisAutoConfiguration() {
        log.info("=== AnySDK Redis 自动配置已加载 ===");
    }

    /**
     * 创建Redis事件管理器bean
     */
    @Bean
    @ConditionalOnMissingBean
    public RedisEventManager redisEventManager() {
        log.info("创建Redis事件管理器");
        return new RedisEventManager();
    }

    /**
     * 创建IRedisService bean
     */
    @Bean
    @ConditionalOnMissingBean
    public IRedisService redisService(RedisProperties redisProperties, RedisEventManager eventManager) {
        log.info("创建Redis服务实例，当前环境: {}", redisProperties.getEnvironment());

        // 使用工厂模式根据环境创建对应的Redis服务实现
        return RedisServiceFactory.createRedisService(redisProperties, eventManager);
    }
}